package org.jeecg.modules.business.third.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTaskMaterial;
import org.jeecg.modules.business.delivery.mapper.WmsDeliveryTaskMapper;
import org.jeecg.modules.business.delivery.mapper.WmsDeliveryTaskMaterialMapper;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.entity.WmsInReceiptMaterialVo;
import org.jeecg.modules.business.third.entity.WmsInTaskMaterialVo;
import org.jeecg.modules.business.third.entity.WmsOutReceiptMaterialVo;
import org.jeecg.modules.business.third.entity.WmsOutTaskMaterialVo;
import org.jeecg.modules.business.third.mapper.InReceiptMapper;
import org.jeecg.modules.business.third.mapper.OutReceiptMapper;
import org.jeecg.modules.business.third.service.IThirdCommonService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.mapper.WmsMoveReceiptMaterialMapper;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-11
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ThirdCommonServiceImpl implements IThirdCommonService {
    private final IWmsInReceiptService wmsInReceiptService;
    private final IWmsInReceiptTaskService wmsInReceiptTaskService;
    private final InReceiptMapper inReceiptMapper;
    private final IWmsOutReceiptService wmsOutReceiptService;
    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    private final OutReceiptMapper outReceiptMapper;
    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final WmsDeliveryTaskMapper wmsDeliveryTaskMapper;
    private final WmsDeliveryTaskMaterialMapper wmsDeliveryTaskMaterialMapper;
    private final IWmsMoveReceiptService wmsMoveReceiptService;
    private final WmsMoveReceiptMaterialMapper wmsMoveReceiptMaterialMapper;

    @Override
    public Map<String, Object> warehouseReceiptMaterialVoes(String taskId, String receiptId, Integer type) {

        if (StringUtils.isBlank(taskId) && StringUtils.isBlank(receiptId)) {
            throw new JeecgBootException("传参全部为空，不能执行");
        }
        WmsInReceiptTask wmsInReceiptTask = null;
        List<WmsInReceiptMaterialVo> wmsInReceiptMaterialVos = null;

        if (type == 1) {
            // 按照单据过账
            // 根据任务id 区分过账类型
            WmsInReceipt wmsInReceipt = null;
            if (StringUtils.isNotBlank(receiptId)) {
                wmsInReceipt = wmsInReceiptService.getById(receiptId);
            }
            if (ObjectUtil.isNotEmpty(wmsInReceipt) && StringUtils.isBlank(taskId)) {
                taskId = wmsInReceipt.getWmsInReceiptTaskId();
            }
            wmsInReceiptTask = wmsInReceiptTaskService.getById(taskId);
            if (StringUtils.isBlank(wmsInReceiptTask.getId())) {
                throw new JeecgBootException("当前任务id，查询无结果，请重新确认");
            }
            wmsInReceiptMaterialVos = inReceiptMapper.listWmsInReceipt(taskId, receiptId);
        } else {
            // 按照任务过账
            // type == 2
            List<WmsInTaskMaterialVo> wmsInTaskMaterialVos = inReceiptMapper.listWmsInTask(taskId);
            // 转化为WmsInReceiptMaterialVo
            wmsInReceiptMaterialVos = new ArrayList<>();
            for (WmsInTaskMaterialVo taskMaterialVo : wmsInTaskMaterialVos) {
                WmsInReceiptMaterialVo wmsInReceiptMaterialVo = new WmsInReceiptMaterialVo();
                BeanUtils.copyProperties(taskMaterialVo, wmsInReceiptMaterialVo);
                wmsInReceiptMaterialVos.add(wmsInReceiptMaterialVo);
            }
            wmsInReceiptTask = wmsInReceiptTaskService.getById(taskId);
        }
        Map<String, Object> map = new HashMap<>();
        List<String> list = Arrays.asList("R101", "R105");
        if (ObjectUtil.isNotNull(wmsInReceiptTask)&& list.contains(wmsInReceiptTask.getDocumentType())){
            WmsDeliveryTask wmsDeliveryTask = wmsDeliveryTaskMapper.selectOne(new LambdaQueryWrapper<WmsDeliveryTask>()
                    .eq(WmsDeliveryTask::getDocumentCode, wmsInReceiptTask.getTransferApplicationOrder())
                    .last("limit 1")
            );
            if (ObjectUtil.isNotNull(wmsDeliveryTask)){
                List<WmsDeliveryTaskMaterial> wmsDeliveryTaskMaterials = wmsDeliveryTaskMaterialMapper.selectList(new LambdaQueryWrapper<WmsDeliveryTaskMaterial>()
                        .eq(WmsDeliveryTaskMaterial::getWmsDeliveryTaskId, wmsDeliveryTask.getId())
                );
                if (CollectionUtil.isNotEmpty(wmsDeliveryTaskMaterials)){
                    map.put("deliveryData",wmsDeliveryTaskMaterials);
                }
            }
        }
        map.put("documentType", wmsInReceiptTask.getDocumentType());
        map.put("taskId", taskId);
        map.put("records", wmsInReceiptMaterialVos);
        return map;
    }

    @Override
    public Map<String, Object> outboundReceiptMaterialVoes(String taskId, String receiptId, Integer type) {
        if (StringUtils.isBlank(taskId) && StringUtils.isBlank(receiptId)) {
            throw new JeecgBootException("传参全部为空，不能执行");
        }
        WmsOutReceiptTask wmsOutReceiptTask = null;
        List<WmsOutReceiptMaterialVo> wmsOutReceiptMaterialVos = null;

        // 按照单据过账
        if (type == 1) {
            // 根据任务id 区分过账类型
            WmsOutReceipt wmsOutReceipt = null;
            if (StringUtils.isNotBlank(receiptId)) {
                wmsOutReceipt = wmsOutReceiptService.getById(receiptId);
            }
            if (ObjectUtil.isNotEmpty(wmsOutReceipt) && StringUtils.isBlank(taskId)) {
                taskId = wmsOutReceipt.getWmsOutReceiptTaskId();
            }
            wmsOutReceiptTask = wmsOutReceiptTaskService.getById(taskId);
            if (StringUtils.isBlank(wmsOutReceiptTask.getId())) {
                throw new JeecgBootException("当前任务id，查询无结果，请重新确认");
            }
            wmsOutReceiptMaterialVos = outReceiptMapper.listWmsOutReceipt(taskId, receiptId);
        } else {
            // 按照任务过账
            // type == 2
            List<WmsOutTaskMaterialVo> wmsOutTaskMaterialVos = outReceiptMapper.listWmsOutTask(taskId);
            // 转化为WmsInReceiptMaterialVo
            wmsOutReceiptMaterialVos = new ArrayList<>();
            for (WmsOutTaskMaterialVo taskMaterialVo : wmsOutTaskMaterialVos) {
                //增加判断，如果已出库数量不为零才过账
                BigDecimal hasOutboundQuantity = Objects.isNull(taskMaterialVo.getHasOutboundQuantity()) ? BigDecimal.ZERO : taskMaterialVo.getHasOutboundQuantity();
                if (hasOutboundQuantity.compareTo(BigDecimal.ZERO) > 0) {
                    WmsOutReceiptMaterialVo outReceiptMaterialVo = new WmsOutReceiptMaterialVo();
                    BeanUtils.copyProperties(taskMaterialVo, outReceiptMaterialVo);
                    wmsOutReceiptMaterialVos.add(outReceiptMaterialVo);
                }
            }
            wmsOutReceiptTask = wmsOutReceiptTaskService.getById(taskId);
        }


        Map<String, Object> map = new HashMap<>();
        map.put("documentType", wmsOutReceiptTask.getDocumentType());
        map.put("taskId", taskId);
        map.put("records", wmsOutReceiptMaterialVos);
        return map;
    }

    @Override
    public Map<String, Object> deliveryMaterialVoes(String taskId, String receiptId, Integer type) {
        if (StringUtils.isBlank(taskId) && StringUtils.isBlank(receiptId)) {
            throw new JeecgBootException("传参全部为空，不能执行");
        }
        WmsDeliveryTask wmsDeliveryTask = null;
        List<WmsOutReceiptMaterialVo> wmsOutReceiptMaterialVos = null;
        List<WmsDeliveryTaskMaterial> list = new ArrayList<>();
        // 按照单据过账
        if (type == 1) {
            // 根据任务id 区分过账类型
            WmsDeliveryTaskMaterial wmsDeliveryTaskMaterial = null;
            if (StringUtils.isNotBlank(receiptId)) {
                wmsDeliveryTaskMaterial = wmsDeliveryTaskMaterialMapper.selectById(receiptId);
                list.add(wmsDeliveryTaskMaterial);
            }
            if (ObjectUtil.isNotEmpty(wmsDeliveryTaskMaterial) && StringUtils.isBlank(taskId)) {
                taskId = wmsDeliveryTaskMaterial.getWmsDeliveryTaskId();
            }
            wmsDeliveryTask = wmsDeliveryTaskMapper.selectById(taskId);
            if (StringUtils.isBlank(wmsDeliveryTask.getId())) {
                throw new JeecgBootException("当前任务id，查询无结果，请重新确认");
            }
        } else {
            // 按照任务过账
            // type == 2

            wmsDeliveryTask = wmsDeliveryTaskMapper.selectById(taskId);
            list = wmsDeliveryTaskMaterialMapper.selectList(new LambdaQueryWrapper<WmsDeliveryTaskMaterial>().eq(WmsDeliveryTaskMaterial::getWmsDeliveryTaskId, taskId));
        }


        Map<String, Object> map = new HashMap<>();
        map.put("documentType", wmsDeliveryTask.getDocumentType());
        map.put("taskId", taskId);
        map.put("records", list);
        return map;
    }

    @Override
    public Map<String, Object> moveMaterialVoes(String receiptId) {
        if (StringUtils.isBlank(receiptId)) {
            throw new JeecgBootException("传参全部为空，不能执行");
        }

        WmsMoveReceipt moveReceipt = wmsMoveReceiptService.getById(receiptId);
        List<WmsMoveReceiptMaterial> wmsMoveReceiptMaterials = wmsMoveReceiptMaterialMapper.selectList(new LambdaQueryWrapper<WmsMoveReceiptMaterial>().eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, receiptId));

        Map<String, Object> map = new HashMap<>();
        map.put("documentType", moveReceipt.getDocumentType());
        map.put("receiptId", receiptId);
        map.put("records", wmsMoveReceiptMaterials);
        map.put("moveReceipt", moveReceipt);
        return map;
    }

    @Override
    public Optional<WmsOutReceiptTaskMaterial> getWmsOutReceiptTaskMaterials(String taskId) {
        List<WmsOutReceiptTaskMaterial> taskMaterials = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, taskId));
        Optional<WmsOutReceiptTaskMaterial> taskMaterial = taskMaterials.stream().filter(t -> org.apache.commons.lang3.StringUtils.isNotBlank(t.getExtraText())).findFirst();
        return taskMaterial;
    }

    @Override
    public Optional<WmsInReceiptTaskMaterial> getWmsInReceiptTaskMaterials(String taskId) {
        List<WmsInReceiptTaskMaterial> taskMaterials = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskId));
        Optional<WmsInReceiptTaskMaterial> taskMaterial = taskMaterials.stream().filter(t -> org.apache.commons.lang3.StringUtils.isNotBlank(t.getExtraText())).findFirst();
        return taskMaterial;
    }
}
